package com.kzw.service;

import org.apache.commons.codec.digest.DigestUtils;

import com.kzw.dao.UserDao;
import com.kzw.entity.User;

import my.db.Page;

public class UserService {

	private UserDao dao = new UserDao();

	public User validUser(String name, String passwd) {
		User user = dao.findByName(name);
		if (user != null) {
			String pwd = DigestUtils.md5Hex(passwd);
			if (user.getPasswd().equals(pwd)) {
				return user;
			}
		}
		return null;
	}
	
	public User FindUser(Long id) {
		  return dao.findByID(id);
	}
	
	
	public User RegistUser(String name, String passwd,String email) {
		User user = dao.findByName(name);
		if (user == null) {
			String pwd = DigestUtils.md5Hex(passwd);
			dao.InsertUser(name, pwd, email);
			user = dao.findByName(name);
			return user;
			}
		return null;
	}
	
	public User RegistUser(String name, String passwd,String email,Long money) {
		User user = dao.findByName(name);
		if (user == null) {
			String pwd = DigestUtils.md5Hex(passwd);
			dao.InsertUser(name, pwd, email,money);
			user = dao.findByName(name);
			return user;
			}
		return null;
	}
	
	public void InsertUser(String name,String pwd,String email) {
		dao.InsertUser(name, pwd, email);
	}
	public void update(User a) {
		dao.update(a);
	}
	public void delete(long id) {
		dao.delete(id);		
	}
	public Boolean validUserExist(String name) {
		User user = dao.findByName(name);
		if (user != null) {
			return false;
		}
		return true;
	}
	
	public Page<User> list(Page<User> page, String uname) {
		return dao.findPage(page,uname);
	}
	public User get(Long id) {
		return dao.get(id);
	}
	
	
}
